package com.atguigu.bigdata0706.redis;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 1.创建客户端，建立连接
 * 2.调用api
 * 3.关闭
 */
public class RedisDemo {

    private static String hostname = "hadoop102";
    private static int port = 6379;

    private static JedisPool pool = null;

    //创建客户端
    public static Jedis getJedis(){
        Jedis jedis = new Jedis(hostname , port);
        return jedis;
    }


    //创建连接池客户端
    public static Jedis getJedisFromPool(){
        if(pool == null){
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            //连接数
            jedisPoolConfig.setMaxTotal(10);
            //最大闲置的连接数
            jedisPoolConfig.setMaxIdle(5);
            //最小闲置的连接数
            jedisPoolConfig.setMinIdle(5);
            jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
            jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
            jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong
            pool = new JedisPool(jedisPoolConfig,hostname,port);
        }
        Jedis jedis = pool.getResource();

        return jedis;
    }

    public static void testString(){

//        Jedis jedis = getJedis();
        Jedis jedis = getJedisFromPool();
        jedis.set("username","wangwu");
        System.out.println(jedis.get("username"));
        jedis.close();

    }



    public static void testList(){

    }


    public static void testSet(){

    }


    public static void testZset(){

    }


    public static void testHash(){

    }

    public static void main(String[] args) {
//        Jedis jedis = getJedis();
//        String ping = jedis.ping();
//        System.out.println(ping);
//        jedis.close();
        testString();
    }

}
